package com.pubinfo.passbook.common.service.dedicated.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.common.entity.TFileEnclosureFtp;
import com.pubinfo.passbook.common.model.dto.MDM.TSourceArtificialParam;
import com.pubinfo.passbook.common.model.dto.file.ArtificialParam;
import com.pubinfo.passbook.common.model.dto.financial.PassbookDetailsParam;
import com.pubinfo.passbook.common.model.http.baseEntity.BaseBody;
import com.pubinfo.passbook.common.model.http.baseEntity.Baseinfo;
import com.pubinfo.passbook.common.model.http.baseEntity.Message;
import com.pubinfo.passbook.common.model.http.baseEntity.request.IRequest;
import com.pubinfo.passbook.common.model.http.baseEntity.request.MessageList;
import com.pubinfo.passbook.common.model.http.baseEntity.request.RequestEnvelope;
import com.pubinfo.passbook.common.model.http.request.GainWriteoffInstStatus.GainWriteoffInstStatusItems;
import com.pubinfo.passbook.common.model.http.request.GetWriteoffBaseData.GetWriteoffBaseDataBody;
import com.pubinfo.passbook.common.model.http.request.GetWriteoffBaseData.GetWriteoffBaseDataItems;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffBaseData.WriteoffBaseDataEnvelope;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffBaseData.WriteoffBaseDataMessage;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffInstStatus.WriteoffInstStatusMessage;
import com.pubinfo.passbook.common.model.ruler.Reimbursement;
import com.pubinfo.passbook.common.model.ruler.ReimbursementParam;
import com.pubinfo.passbook.common.service.dedicated.DedicatedService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DedicatedServiceImpl implements DedicatedService {
  @Value("${post.url}")
  String postUrl;
  @Value("${post.http-port}")
  String httpPort;
  
  @SneakyThrows
  private String sendPost(String s, String url) {
    //Unirest.setTimeouts(5000, 5000);
    HttpResponse<String> response = null;
    // try {
    response = Unirest.post(url)
        .header("User-Agent", "apifox/1.0.0 (https://www.apifox.cn)")
        .header("Content-Type", "application/json")
        .header("Accept-Encoding", "gzip, deflate, br")
        .body(s)
        .asString();
//    } catch (UnirestException e) {
//      throw new ServiceException("调用服务失败", e);
//    }
    // Unirest.
    return response.getBody();
  }
  
  private String sendHttpPost(String s, String url) {
    
    return sendPost(s, postUrl + ":" + httpPort + url);
  }
  
  @SneakyThrows
  @Override
  public WriteoffBaseDataMessage getWriteoffBaseDataItem(List<GetWriteoffBaseDataItems> account) {
    //String postUrl = config.getEsb().getUrl() + config.getEsb().getApi().getGetWriteoffBaseData();
    BaseBody baseBody = new BaseBody();
    Message message = new Message(new MessageList(account));
    baseBody.setBaseinfo(Baseinfo.createBaseinfo("getWriteoffBaseDataItem", new Date()));
    baseBody.setMessage(message);
    RequestEnvelope requestEnvelope = new RequestEnvelope();
    GetWriteoffBaseDataBody body = new GetWriteoffBaseDataBody();
    IRequest IRequest = new IRequest();
    IRequest.setBaseBody(baseBody);
    body.setIRequest(IRequest);
    requestEnvelope.setRequestBody(body);
    //发送post请求
    String s = getWriteoffBaseDataItem(new XmlMapper().writeValueAsString(requestEnvelope));
    //处理&#60;和&#62;的乱码
    s = s.replaceAll("&#60;", "<").replaceAll("&#62;", ">");
    
    System.out.println(s);
    
    WriteoffBaseDataEnvelope WriteoffBaseDataEnvelope = new XmlMapper().readValue(s, WriteoffBaseDataEnvelope.class);
    
    WriteoffBaseDataMessage writeoffBaseDataMessage = new XmlMapper().readValue(WriteoffBaseDataEnvelope.getBody().getEResponse().getBaseBody().getMessage().getMessage(), WriteoffBaseDataMessage.class);
    
    return writeoffBaseDataMessage;
  }
  
  @Override
  public String createWriteoff(String xml) {
    
    return sendHttpPost(xml, "/api/mss/createWriteoff");
  }
  
  @Override
  public String getWriteoffBaseDataItem(String xml) {
    
    return sendHttpPost(xml, "/api/mss/getWriteoffBaseDataItem");
  }
  
  @Override
  public WriteoffInstStatusMessage getWriteoffInstStatusBodyItem(List<GainWriteoffInstStatusItems> account) {
    
    String reply = sendHttpPost(JSONObject.toJSONString(account), "/api/mss/getWriteoffInstStatus");
    return JSONObject.toJavaObject(JSON.parseObject(reply), WriteoffInstStatusMessage.class);
  }
  
  @Override
  public String getSapNumber(String xml) {
    
    return sendHttpPost(xml, "/api/mss/createWriteoff");
  }
  
  @Override
  public TFileEnclosureFtp createExcelByClass(List<Reimbursement> reimbursement) {
    
    String reply = sendHttpPost(JSONObject.toJSONString(reimbursement), "/api/mss/createExcelByClass");
    return JSONObject.toJavaObject(JSON.parseObject(reply), TFileEnclosureFtp.class);
    
  }

  @Override
  public TFileEnclosureFtp createShenGongSiExcel(ReimbursementParam reimbursementParam) {
    String reply = sendHttpPost(JSONObject.toJSONString(reimbursementParam), "/api/mss/createShenGongSiExcel");
    return JSONObject.toJavaObject(JSON.parseObject(reply), TFileEnclosureFtp.class);
  }

  @Override
  public String getPassbookDetails(PassbookDetailsParam param) {
    return sendHttpPost(JSONObject.toJSONString(param), "/api/passbook/detailsByInfo");
  }


}
